/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created by dnoakes on 3-Oct-2002 10:12:05 using IntelliJ IDEA.
 */

package au.com.lastweekend.jim.io.exif.makernote.nikon;

import au.com.lastweekend.jim.io.metadata.Directory;
import au.com.lastweekend.jim.io.metadata.MetadataException;
import au.com.lastweekend.jim.io.metadata.AbstractTagDescriptor;
import au.com.lastweekend.jim.util.Rational;

/**
 * There are 3 formats of Nikon's MakerNote. MakerNote of E700/E800/E900/E900S/E910/E950 starts from ASCII string "Nikon". Data
 * format is the same as IFD, but it starts from offset 0x08. This is the same as Olympus except start string. Example of actual
 * data structure is shown below.
 * 
 * <pre><code>
 * 
 *  :0000: 4E 69 6B 6F 6E 00 02 00-00 00 4D 4D 00 2A 00 00 Nikon....MM.*...
 *  :0010: 00 08 00 1E 00 01 00 07-00 00 00 04 30 32 30 30 ............0200
 *  
 * </code></pre>
 * 
 * @version $Id: NikonType3MakernoteDescriptor.java,v 1.2 2005/06/09 11:15:38 ggardner Exp $
 * @author <a href="mailto:grant@lastweekend.com.au"> <Grant Gardner> </a>
 */
public class NikonType3MakernoteDescriptor extends AbstractTagDescriptor {

    public NikonType3MakernoteDescriptor(Directory directory) {

        super(directory);
    }

    @Override
    public String getDescription(int tagType) throws MetadataException {

        switch (tagType) {
            case NikonType3MakernoteDirectory.TAG_NIKON_TYPE3_LENS :
                return getLensDescription();
            case NikonType3MakernoteDirectory.TAG_NIKON_TYPE3_CAMERA_HUE_ADJUSTMENT :
                return getHueAdjustmentDescription();
            case NikonType3MakernoteDirectory.TAG_NIKON_TYPE3_CAMERA_COLOR_MODE :
                return getColorModeDescription();
            default :
                return getDirectory().getString(tagType);
        }
    }

    public String getLensDescription() throws MetadataException {

        if (!getDirectory().containsTag(NikonType3MakernoteDirectory.TAG_NIKON_TYPE3_LENS)) {
            return null;
        }

        Rational[] lensValues = getDirectory().getRationalArray(NikonType3MakernoteDirectory.TAG_NIKON_TYPE3_LENS);

        if (lensValues.length != 4) {
            return getDirectory().getString(NikonType3MakernoteDirectory.TAG_NIKON_TYPE3_LENS);
        }
        StringBuffer description = new StringBuffer();
        description.append(lensValues[0].intValue());
        description.append('-');
        description.append(lensValues[1].intValue());
        description.append("mm f/");
        description.append(lensValues[2].floatValue());
        description.append('-');
        description.append(lensValues[3].floatValue());

        return description.toString();
    }

    public String getHueAdjustmentDescription() {

        if (!getDirectory().containsTag(NikonType3MakernoteDirectory.TAG_NIKON_TYPE3_CAMERA_HUE_ADJUSTMENT)) {
            return null;
        }

        return getDirectory().getString(NikonType3MakernoteDirectory.TAG_NIKON_TYPE3_CAMERA_HUE_ADJUSTMENT) + " degrees";
    }

    public String getColorModeDescription() {

        if (!getDirectory().containsTag(NikonType3MakernoteDirectory.TAG_NIKON_TYPE3_CAMERA_COLOR_MODE)) {
            return null;
        }

        String raw = getDirectory().getString(NikonType3MakernoteDirectory.TAG_NIKON_TYPE3_CAMERA_COLOR_MODE);
        if (raw.startsWith("MODE1")) {
            return "Mode I (sRGB)";
        }

        return raw;
    }

    @Override
    public boolean isTagUpdateable(int tagType) {

        return false;
    }
}